package us.icitap.commandcenter.ui.dispatcher;

import java.util.Date;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.Officer;
import us.icitap.commandcenter.business.ejb.persistence.OrgStructure;
import us.icitap.commandcenter.business.ejb.persistence.UniqueNumber;
import us.icitap.commandcenter.listeners.FillListener;
import us.icitap.commandcenter.listeners.ManageCallListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustButton;
import us.icitap.commandcenter.ui.custom.CustTextArea;
import us.icitap.commandcenter.ui.custom.CustVerticalLayout;
import us.icitap.commandcenter.utils.OfficerComponent;
import us.icitap.utils.Result;

public class UniqueNumberContent extends CustVerticalLayout{

	private final CustTextArea unNotesTextArea = new CustTextArea("Shenime mbi numrin unik");
	

	private Officer selectedOfficer;
	

	private OfficerComponent officerComponent = new OfficerComponent();
	
	public void _init(){
		officerComponent.addFillListener(new FillListener() {	
			@Override
			public <T> void fillFinished(T result) {
				if(result == null) selectedOfficer = null;
				else
				selectedOfficer = (Officer) result;
			}
		});
		
		this.addComponent(officerComponent);
			
		this.addComponent(unNotesTextArea);
		unNotesTextArea.setWidth("500px");
		unNotesTextArea.setRows(4);
		
	}
	public UniqueNumberContent(String caption) {

		this.setSizeUndefined();
		this.setCaption(caption);
		this.setMargin(false);
		this.setSpacing(true);
		
		_init();
		this.addStyleName("cust-margin-top-4");

	}
	
	public UniqueNumberContent() {

		this.setMargin(true);
		this.setSpacing(true);
		
		_init();

	}
	
	public Result<UniqueNumber> createUniqueNumberObject(){
		try {
			if(this.selectedOfficer == null) return new Result<UniqueNumber>(null, false, "Duhet te zgjidhni oficerin qe kerkon numrin unik");
			UniqueNumber un = new UniqueNumber();
			un.setRequestingOfficer(selectedOfficer);
			un.setOrgStructure(selectedOfficer.getOrgStructure());
			un.setDescription(this.unNotesTextArea.getValue());
			un.setRecordUser(CommandCenterUI.getUser().getUsername());
			un.setRecordDate(new Date());
			
			return new Result<UniqueNumber>(un, true, null);
		} catch (Exception e) {
			return new Result<UniqueNumber>(null, false, e.getMessage());
		}
	}
	
	public void resetContent(){
		officerComponent.setOfficer("", "");
		selectedOfficer = null;
		unNotesTextArea.setValue(null);
	}
}
